package com.example.book.in.service;

import com.example.book.base.BaseService;
import com.example.book.common.utils.Constants;
import com.example.book.crawler.dao.CrawlerContentDao;
import com.example.book.crawler.entity.CrawlerContent;
import com.example.book.crawler.pageprocessor.CatalogPageProcessor;
import com.example.book.crawler.pageprocessor.CategoryPageProcessor;
import com.example.book.crawler.pageprocessor.ContentPageProcessor;
import com.example.book.in.dao.BookContentDao;
import com.example.book.in.dao.BookInfoDao;
import com.example.book.in.entity.BookCatalog;
import com.example.book.in.dao.BookCatalogDao;

import com.example.book.in.entity.BookContent;
import com.example.book.in.entity.BookInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.proxy.Proxy;
import us.codecraft.webmagic.proxy.SimpleProxyProvider;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author hcj
 * @date 2019-10-27 22:24:09
 */
@Service
public class BookCatalogService extends BaseService<BookCatalog, BookCatalogDao> {

    @Autowired
    private CrawlerContentDao crawlerContentDao;
    @Autowired
    private BookContentDao bookContentDao;
    @Autowired
    private BookCatalogDao bookCatalogDao;
    @Autowired
    private BookInfoDao bookInfoDao;

    /**
     * 查询上一章 或者 下一章数据
     *
     * @param catalog
     * @return
     */
    public BookCatalog previousChapterOrNextChapter(BookCatalog catalog) {
        return dao.previousChapterOrNextChapter(catalog);
    }

    /**
     * 查询第一章目录数据
     *
     * @param catalog
     * @return
     */
    public BookCatalog selectOrderByIndexAsc(BookCatalog catalog) {
        return dao.selectOrderByIndexAsc(catalog);
    }

    /**
     * 查询最后一章目录数据
     *
     * @param catalog
     * @return
     */
    public BookCatalog selectOrderByIndexDesc(BookCatalog catalog) {
        return dao.selectOrderByIndexDesc(catalog);
    }

    /**
     * 书籍最终的存储位置 目录保存 内容保存
     * 保存目录信息 并且处理采集目录内容数据状态
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void saveCatalogByStatus(CrawlerContent crawlerContent, BookCatalog bookCatalog, BookContent bookContent) {

        // 更新以处理标志
        crawlerContent.setIsCarriedOut("1");
        crawlerContentDao.update(crawlerContent);
        // 保存目录信息
        bookCatalog.setId(Constants.uuid());
        bookCatalog.setCreateDate(new Date());
        bookCatalog.setUpdateDate(new Date());
        bookCatalog.setDelFlag("0");
        bookCatalogDao.insert(bookCatalog);
        // 保存内容
        bookContent.setBookCatalogId(bookCatalog.getId());
        bookContentDao.insert(bookContent);
        // 检测书籍是否上架状态
        BookInfo info = new BookInfo();
        info.setId(crawlerContent.getBookInfoId());
        BookInfo bi = bookInfoDao.get(info);
        if (bi != null) {
            if (!bi.getStatus().equals("1")) {
                bi.setStatus("1");
                bookInfoDao.update(bi);
            }
        }
    }
}